package com.learner.ucenter.controller.api;

import com.learner.pojo.user.vo.HistoryMsgRequestVo;
import com.learner.pojo.user.vo.IsReadMessageRequestVo;
import com.learner.pojo.user.vo.SingleHistoryResultVo;
import com.learner.pojo.user.vo.SingleMessageResultVo;
import com.learner.ucenter.service.SingleMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ice2020x
 * @Date: 2023/2/27
 * @Description: 单对单聊天消息控制器
 **/
@RequestMapping("/api/ucenter/singleMessage")
@RestController
@Slf4j
public class SingleMessageController {
    @Resource
    private SingleMessageService singleMessageService;

    /**
     * 获取好友之间的最后一条聊天记录
     *
     * @param roomId 房间id
     * @return 结果
     */
    @GetMapping("/getLastMessage")
    public SingleMessageResultVo getLastMessage(String roomId) {
        SingleMessageResultVo lastMessage = singleMessageService.getLastMessage(roomId);
        log.info("获取好友之间的最后一条聊天记录,roomId:{},查询到的单聊信息为:{}", roomId, lastMessage);
        return lastMessage;
    }

    /**
     * 获取好友间的单聊信息
     *
     * @param roomId    房间id
     * @param pageIndex pageIndex
     * @param pageSize  pageSize
     * @return 结果
     */
    @GetMapping("/getRecentSingleMessages")
    public List<SingleMessageResultVo> getRecentSingleMessages(String roomId, Integer pageIndex, Integer pageSize) {
        log.info("获取好友间的单聊信息,roomId:{},pageIndex:{},pageSize:{}", roomId, pageIndex, pageSize);
        List<SingleMessageResultVo> recentMessage = singleMessageService.getRecentMessage(roomId, pageIndex, pageSize);
        log.info("获取好友间的单聊信息:{}", recentMessage);
        return recentMessage;
    }

    /**
     * 修改状态标记为已读
     *
     * @param ivo 参数
     */
    @PostMapping("/isRead")
    public void userIsReadMessage(@RequestBody IsReadMessageRequestVo ivo) {
        singleMessageService.userIsReadMessage(ivo);
    }

    /**
     * 获取单聊的历史记录
     *
     * @param historyMsgVo 房间id
     * @return 结果
     */
    @PostMapping("/historyMessage")
    public SingleHistoryResultVo getSingleHistoryMessages(@RequestBody HistoryMsgRequestVo historyMsgVo) {
        log.info("查看历史消息的请求参数为{}", historyMsgVo);
        SingleHistoryResultVo singleHistoryMsg = singleMessageService.getSingleHistoryMsg(historyMsgVo);
        log.info("获取单聊的历史记录:{}", singleHistoryMsg);
        return singleHistoryMsg;
    }
}
